package com.co.vote.web.controller.home;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.co.common.utils.PageReq;
import com.co.server.entity.vote.TChoice;
import com.co.server.entity.vote.TClass;
import com.co.server.entity.vote.TLabel;
import com.co.server.entity.vote.TVote;
import com.co.server.entity.vote.TVoteItem;
import com.co.server.entity.vote.TVoteLabel;
import com.co.server.entity.vote.aide.TChoiceQuery;
import com.co.server.entity.vote.aide.TClassQuery;
import com.co.server.entity.vote.aide.TLabelQuery;
import com.co.server.entity.vote.aide.TVoteItemQuery;
import com.co.server.entity.vote.aide.TVoteLabelQuery;
import com.co.server.entity.vote.aide.TVoteQuery;
import com.co.server.service.user.TUsersService;
import com.co.server.service.user.impl.UsersSession;
import com.co.server.service.vote.TChoiceService;
import com.co.server.service.vote.TClassService;
import com.co.server.service.vote.TLabelService;
import com.co.server.service.vote.TVoteItemService;
import com.co.server.service.vote.TVoteLabelService;
import com.co.server.service.vote.TVoteService;
import com.co.server.service.vote.TVoteUserService;
import com.co.vote.utils.ClientUtil;
import com.co.vote.utils.SessionUtil;


@Controller
public class HomeController {
	
	@Resource
	TUsersService tUsersService;
	@Resource
	TVoteService  tVoteService;
	@Resource
	TVoteItemService  tVoteItemService;
	@Resource
	TLabelService  tLabelService;
	@Resource
	TClassService  tClassService;
	@Resource
	TVoteLabelService  tVoteLabelService;
	@Resource
	TChoiceService  tChoiceService;
	@Resource
	TVoteUserService  tVoteUserService;
	
	@RequestMapping(value = { "/", "/home" })
	public String index(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) {	
		UsersSession usersSession = SessionUtil.getUserSession(session);
		PageReq pageReq = new PageReq();
		TVote tVote = tVoteService.getDefaultVote(usersSession,pageReq,ClientUtil.getIp(request));
		model.addAttribute("vote", tVote);
		Integer voteId = tVote.getId();
		//投票的选项
		TVoteItemQuery tVoteItemQuery = new TVoteItemQuery();
		tVoteItemQuery.setVoteId(voteId);
		List<TVoteItem> voteItemList = tVoteItemService.queryList(tVoteItemQuery);
		model.addAttribute("voteItemList", voteItemList);
		//设置百分数
		tVoteItemService.setPrecent(voteId, voteItemList);
		
		//查看是否已经投票
		Integer userId= SessionUtil.getUserId(session);
		String ip = ClientUtil.getIp(request);
		Boolean voteFlg = tVoteService.getVoteFlg(userId,ip,voteId);
		if(voteFlg){
			model.addAttribute("hasvote", true);
		}
		
		//本投票的关键词
		TVoteLabelQuery tVoteLabelQuery = new TVoteLabelQuery();
		tVoteLabelQuery.setVoteId(voteId);
		List<TVoteLabel> voteLabelList = tVoteLabelService.queryList(tVoteLabelQuery);
		model.addAttribute("voteLabelList", voteLabelList);
		
		//全部分类列表
		List<TClass> classList = tClassService.queryList(new TClassQuery());
		model.addAttribute("classList", classList);
		
		//选取10个最热的关键词
		PageReq pageReq4Label = new PageReq();
		pageReq4Label.setPageSize(10);
		pageReq4Label.setSort(new Sort(Direction.DESC,"t.vote_num"));
		Page<TLabel> labelPage = tLabelService.queryPageList(new TLabelQuery(), pageReq4Label);
		model.addAttribute("labelList", labelPage.getContent());
		//最新
		pageReq.setPageSize(10);
		pageReq.setSort(new Sort(Direction.DESC,"t.id"));
		Page<TVote> latestPageVote = tVoteService.queryPageList(new TVoteQuery(), pageReq);
		model.addAttribute("latestVoteList", latestPageVote.getContent());
		//最热	
		pageReq.setSort(new Sort(Direction.DESC,"t.vote_num"));
		Page<TVote> hotPageVote = tVoteService.queryPageList(new TVoteQuery(), pageReq);
		model.addAttribute("hotVoteList", hotPageVote.getContent());
		
		TChoiceQuery tChoiceQuery = new TChoiceQuery();
		//查询是否已经点赞
		tChoiceQuery.setIp(ClientUtil.getIp(request));
		tChoiceQuery.setUserId(SessionUtil.getUserId(session));
		tChoiceQuery.setVoteId(voteId);
		TChoice choiceOld = tChoiceService.queryOne(tChoiceQuery);
		if(choiceOld!=null){
			model.addAttribute("knockDone", true);
		}
		
		return "/home/index";
	}

	
}
